package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void savecategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        Long id = BaseContext.getCurrentId();
//        category.setCreateUser(id);
//        category.setUpdateUser(id);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.savecategory(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        System.out.println(page.getResult());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void status(int status, Long id) {
        Category category=categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        Long id = BaseContext.getCurrentId();
        category.setUpdateUser(id);
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public List<CategoryDTO> list() {
        return categoryMapper.list();
    }

    @Override
    public List<Category> selectByType(Long type) {
        if(type==null){
//            如果类型是空,说明需要查询所有类型的分类
            return categoryMapper.all();
        }
//        List<Category> categories = categoryMapper.selectByType(type);
        return categoryMapper.selectByType(type);
    }
}
